/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

package app.models.brand;

import app.Const;
import app.dtos.ShowBrand;
import app.models.DaoKit;
import com.github.sog.annotation.TableBind;
import com.github.sog.config.StringPool;
import com.github.sog.controller.datatables.core.ColumnDef;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.libs.AppFunc;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;

/**
 * <p>
 * The table ol_brand mapping model.
 * </p>
 */
@TableBind(tableName = "ol_brand")
public class Brand extends Model<Brand> {

    /**
     * The public dao.
     */
    public static final  Brand dao              = new Brand();
    private static final long  serialVersionUID = 1L;

    public List<Brand> list() {
        return find(SqlKit.sql("brand.list"));
    }
    public List<Brand> findByDining() {
        return find(SqlKit.sql("brand.findByDining"), Const.REPAST);
    }

    public Page<Brand> datatable(DatatablesCriterias criterias, String name, int floor, int format) {
        StringBuilder sb = new StringBuilder(" WHERE 1=1 ");
        if (!Strings.isNullOrEmpty(name)) {
            sb.append(" AND (b.name like '%").append(name).append("%' OR e_name like '%").append(name).append("%')");
        }
        if (floor > 0) {
            sb.append(" AND b.floor=").append(floor);
        }
        if (format > 0) {
            sb.append(" AND b.format=").append(format);
        }

        int pageSize = criterias.getDisplaySize();
        int start = criterias.getDisplayStart() / pageSize + 1;
        final List<ColumnDef> sortingColumnDefs = criterias.getSortingColumnDefs();
        String where = " FROM ol_brand b INNER JOIN olb_floor f on b.floor = f.id ";
        where += sb.toString();

        if (sortingColumnDefs != null && !sortingColumnDefs.isEmpty()) {
            StringBuilder orderBy = new StringBuilder();
            for (ColumnDef sortingColumnDef : sortingColumnDefs)
                if (sortingColumnDef.isSortable()) {
                    orderBy.append(sortingColumnDef.getName()).append(StringPool.SPACE).append(sortingColumnDef.getSortDirection().toString());
                }
            final String byColumns = orderBy.toString();
            if (!Strings.isNullOrEmpty(byColumns)) {
                where += " ORDER BY " + byColumns;
            }
        }
        if (!where.contains("ORDER")) {
            where += " ORDER BY floor";
        }

        return paginate(start, pageSize, SqlKit.sql("brand.columns"), where);
    }

    public ShowBrand findWithStatById(long id) {
        Record record = Db.findFirst(SqlKit.sql("brand.findWithStatById"), id);
        ShowBrand showBrand = new ShowBrand();
        if (record != null) {
            showBrand.brandId = record.getNumber(AppFunc.TABLE_PK_COLUMN).longValue();
            showBrand.brandName = record.getStr("name");
            showBrand.ename = record.getStr("e_name");
            showBrand.contact = record.getStr("contact");
            showBrand.floor = record.getInt("floor");
            showBrand.floorName = record.getStr("floorName");
            showBrand.logo = record.getStr("logo");
            showBrand.location = record.getStr("location");
            showBrand.picture = record.getStr("picture");
            showBrand.description = record.getStr("description");
            showBrand.stats = (record.getNumber("stats") == null ? 0 : record.getNumber("stats").intValue());
        }
        return showBrand;
    }

    public List<Brand> findbyFloor(long floor) {
        return find(SqlKit.sql("brand.findByFloor"), floor);
    }

    public List<Brand> findByFormat(long format) {
        return find(SqlKit.sql("brand.findByFormat"), format);
    }

    public List<Brand> findByCatetory(String category) {
        return find(SqlKit.sql("brand.findByCatetory"), category);
    }

    public List<Brand> findByCatetoryAndBooking(String category, boolean booking) {
        return find(SqlKit.sql("brand.findByCatetoryAndBooking"), category, booking);
    }

    public List<Brand> findByFormatAndBooking(Long format) {
        return find(SqlKit.sql("brand.findByFormatAndBooking"), format);
    }

    public Page<Record> datatableWithDining(DatatablesCriterias criterias) {
        List<Object> params = Lists.newArrayList();
        params.add(Const.REPAST);
        return DaoKit.paginate("brand_dining", criterias, params);
    }

    public Brand findWithFloor(long id) {
        return findFirst(SqlKit.sql("brand.findWithFloor"), id);
    }

    public Brand findWithFloorById(long brand_id) {
        return findFirst(SqlKit.sql("brand.findWithFloorById"), brand_id);
    }
}